/*============================================================================*/

* Do: Regressions Part 2 - ENAHO
* Last update: 09.05.2023

/*============================================================================*/

* SECTION: FOLDERS
* ==============================================================================

*------------------------ SUBFOLDERS (DO NOT CHANGE)
gl in "$root/Final"
gl analysis "$root/Analysis"
*gl analysis "c:/data"

* ==============================================================================
* SECTION: REGRESSIONS
* ==============================================================================
cap log close
log using "regressions_part2", replace

use "$in/data_regressions_enaho", clear

recode occupation 0=., gen(occ1)
replace occ1 = 12 - occ1

xtset, clear

* Outcomes

local outcome problem_emp problem_corrupt log_crime_violent log_crime_nonviolent problem_crime employment_status formalemploy occ1 lnwage1 lnhhinc lnhhexp 

local outemp employment_status formalemploy occ1 lnwage1 lnhhinc lnhhexp 

* Covariates

local indvchar c.age##c.age ib1.education_group i.marital_status hhsize  

local work formalemploy i.occupation

local vztype logshare_combined_std logsharevz totptp_std

local iv c.logsharevz07#(i2015.year i2016.year i2017.year i2018.year i2019.year i2020.year)

local ivp c.logsharevz07#(i2009.year i2010.year i2011.year i2012.year i2013.year i2014.year)

/*
egen  maxvzshare = max(sharevz), by( nombprov )
drop if maxvzshare < 0.004
*/


*------------------------ DESCRIPTIVES 

su `outcome' `indvchar' `work' sharevz totnumvz_prov totptp_prov wage1, sep(0)
su `outcome' `indvchar' `work' sharevz totnumvz_prov totptp_prov wage1 if year==2018 & month==12, sep(0)


*------------------------ REGRESSIONS

* first-stage

forv i = 0/2 {

if `i'~=2 {
local sex "if female==`i'"
}

else {
local sex "female"
}

reghdfe logsharevz `iv' `indvchar' `work' `sex', cluster(province_id) absorb(province_id i.month##i.year c.time#i.province_id)
testparm `iv', equal

foreach y of local outcome {

		if "`y'" == "problem_emp" { 
		local ra replace 
		}
	
		else { 
		local ra append 
		}

	 if  ~inlist("`y'", "employment_status", "lnwage1", "lnhhinc", "lnhhexp", "formalemploy", "occ1") { 

		if `i'==2 {

	 	reghdfe `y' logsharevz `indvchar' `work' `sex', cluster(province_id) absorb(province_id i.month##i.year) 
			outreg2 using "$analysis/part2_1`i'.xls", dec(3) nocons `ra' label ///
				addtext(Model, OLS, Region FE, Yes, Year FE, Yes, District FE, Yes, DistrictXTime trends, No)

		reghdfe `y' logsharevz `indvchar' `work'  `sex', cluster(province_id) absorb(province_id c.time#i.province_id i.month##i.year)
			outreg2 using "$analysis/part2_2`i'.xls", dec(3) nocons `ra' label ///
		                addtext(Model, OLS, Region FE, Yes, Year FE, Yes, District FE, Yes, DistrictXTime trends, Yes) 
		}

		ivreghdfe `y' (logsharevz=`iv') `indvchar' `work'  `sex', cluster(province_id) absorb(i.month##i.year province_id c.time#i.province_id)
			outreg2 using "$analysis/part2_3`i'.xls", dec(3) nocons `ra' label ///
				addtext(Model, IV, Region FE, Yes, Year FE, Yes, District FE, Yes, DistrictXTime trends, Yes) 
		}

	if  inlist("`y'", "employment_status", "lnwage1", "lnhhinc", "lnhhexp", "formalemploy", "occ1") {

		if `i'==2 {

		reghdfe `y' logsharevz `indvchar' `sex', cluster(province_id) absorb(province_id i.month##i.year) 
			outreg2 using "$analysis/part2_1`i'.xls", dec(3) nocons `ra' label ///
				addtext(Model, OLS, Region FE, Yes, Year FE, Yes, District FE, Yes, DistrictXTime trends, No)

		reghdfe `y' logsharevz `indvchar' `sex', cluster(province_id) absorb(province_id c.time#i.province_id i.month##i.year)
			outreg2 using "$analysis/part2_2`i'.xls", dec(3) nocons `ra' label ///
		                addtext(Model, OLS, Region FE, Yes, Year FE, Yes, District FE, Yes, DistrictXTime trends, Yes) 
		}
		
		ivreghdfe `y' (logsharevz=`iv') `indvchar' `sex', cluster(province_id) absorb(i.month##i.year province_id c.time#i.province_id)
			outreg2 using "$analysis/part2_3`i'.xls", dec(3) nocons `ra' label ///
				addtext(Model, IV, Region FE, Yes, Year FE, Yes, District FE, Yes, DistrictXTime trends, Yes) 

		}

}
}


* placebo 

foreach y of local outcome {

		if "`y'" == "problem_emp" { 
		local ra replace 
		}
	
		else { 
		local ra append 
		}

	 if  ~inlist("`y'", "employment_status", "lnwage1", "lnhhinc", "lnhhexp", "formalemploy") { 

		reghdfe `y' logsharevz_placebom6 `indvchar' female `work' if year<2015, cluster(province_id) absorb(province_id c.time#i.province_id i.month##i.year)
			outreg2 using "$analysis/part2_2placebo.xls", dec(3) nocons `ra' label ///
		                addtext(Model, OLS, Region FE, Yes, Year FE, Yes, District FE, Yes, DistrictXTime trends, Yes) 

		ivreghdfe `y' (logsharevz_placebom6 = `ivp') `indvchar' female `work' if year<2015, cluster(province_id) absorb(i.month##i.year province_id c.time#i.province_id)
			outreg2 using "$analysis/part2_3placebo.xls", dec(3) nocons `ra' label ///
				addtext(Model, IV, Region FE, Yes, Year FE, Yes, District FE, Yes, DistrictXTime trends, Yes) 
               }
  
  	 else { 
  
  		reghdfe `y' logsharevz_placebom6 `indvchar' female if year<2015, cluster(province_id) absorb(province_id c.time#i.province_id i.month##i.year)
  			outreg2 using "$analysis/part2_2placebo.xls", dec(3) nocons `ra' label ///
  		                addtext(Model, OLS, Region FE, Yes, Year FE, Yes, District FE, Yes, DistrictXTime trends, Yes) 
  
  		ivreghdfe `y' (logsharevz_placebom6 = `ivp') `indvchar' female if year<2015, cluster(province_id) absorb(i.month##i.year province_id c.time#i.province_id)
  			outreg2 using "$analysis/part2_3placebo.xls", dec(3) nocons `ra' label ///
  				addtext(Model, IV, Region FE, Yes, Year FE, Yes, District FE, Yes, DistrictXTime trends, Yes) 
                 }

}

log close

